package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupMagicLampRewardBinding
import com.polaris.live.resp.back_resp.PayerLampReward
import com.polaris.live.resp.back_resp.UserRichCharmLevelRewardExplainVo
import com.polaris.live.utils.image.ImageUtils

/**
 * MagicLampRewardPopupcontext奖励详情
 * @author Created by 天晴 on 2024/11/25 10:28
 * @since 1.0.0
 **/
class MagicLampRewardPopup(
    context: Context,
    val rewards: List<PayerLampReward>,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_magic_lamp_reward
    }

    private lateinit var mBinding: PopupMagicLampRewardBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupMagicLampRewardBinding.bind(popupImplView)
        mBinding.closeImg.setOnSingleClickListener {
            dismiss()
        }
        rewards.forEachIndexed { index, payerLampReward ->
            val rewardCls = listOf(
                mBinding.rewardCl1, mBinding.rewardCl2, mBinding.rewardCl3,
                mBinding.rewardCl4, mBinding.rewardCl5
            )
            val rewardImages = listOf(
                mBinding.rewardImage1, mBinding.rewardImage2, mBinding.rewardImage3,
                mBinding.rewardImage4, mBinding.rewardImage5
            )
            val rewardTips = listOf(
                mBinding.rewardTips1, mBinding.rewardTips2, mBinding.rewardTips3,
                mBinding.rewardTips4, mBinding.rewardTips5
            )

            val rewardNames = listOf(
                mBinding.rewardName1, mBinding.rewardName2, mBinding.rewardName3,
                mBinding.rewardName4, mBinding.rewardName5
            )

            if (index in rewardImages.indices) {
                Glide.with(context)
                    .load(ImageUtils.imgPath(payerLampReward.displayIcon))
                    .into(rewardImages[index])

                rewardTips[index].text = payerLampReward.text
                rewardNames[index].text = payerLampReward.displayText
            }
            rewardCls[index].setOnSingleClickListener {
                if (payerLampReward.decorationType != null) {
                    var effectPhotoUrl = when (payerLampReward.decorationType) {
                        CommonConst.DressUp.LIVE_EFFECT -> {
                            payerLampReward.effectDisplayUrl ?: ""
                        }

                        else -> {
                            payerLampReward.ext ?: ""
                        }
                    }

                    PreviewRichCharmPopup.show(
                        context,
                        UserRichCharmLevelRewardExplainVo(
                            "",
                            payerLampReward.displayIcon,
                            effectPhotoUrl,
                            "{\"width\":${160.autoDp},\"height\":${160.autoDp}}",
                            payerLampReward.decorationType,
                            payerLampReward.displayIcon ?: "",
                            1,
                            ""
                        ),
                        false
                    )
                }

            }
        }
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    companion object {

        fun show(
            context: Context,
            rewards: List<PayerLampReward>,
        ) {

            XPopup.Builder(context)
                .isViewMode(false)
                .hasShadowBg(true)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(MagicLampRewardPopup(context, rewards))
                .show()
        }
    }

}